package edu.xlh.travel.controller.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.pojo.SysLog;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.pojo.WeChatUser_phoneUser;
import edu.xlh.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /*
    * 用户登录：
    * 1，手机号登录
    * 2.用户名登录
    * */
    @RequestMapping("/login")
    public Result login(@RequestBody Map<String , String> hashMap ,String verKey , HttpSession session) {
        return userService.login(hashMap , verKey , session);
    }

    /*
    * 用户注销
    * */
    @RequestMapping("/logout")
    public void logout(HttpSession session , HttpServletRequest request , HttpServletResponse response) throws IOException {
        JSONObject weChatUser = (JSONObject) request.getServletContext().getAttribute("weChatUser");
        if (weChatUser != null) {
            request.getServletContext().removeAttribute("weChatUser");
        }
        Enumeration<String> attributeNames = request.getSession().getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String key = attributeNames.nextElement().toString();
            request.getSession().removeAttribute(key);
        }
        Cookie cookie = new Cookie("nickName" , "");
        cookie.setMaxAge(0);
        cookie.setPath("/dist");
        response.addCookie(cookie);
        response.sendRedirect(request.getContextPath() + "/dist/index");
    }

    /*
    * 用戶注册
    * */

    /*
    * 1.验证电话号码
    * */
    @RequestMapping("/register")
    public Result register(String mobileValue) {
        return userService.findByMobile(mobileValue);
    }

    /*
    * 2.验证邮箱
    * */
    @RequestMapping("/email")
    @ResponseBody
    public Result email(@RequestBody User user) {
        return userService.findByEmail(user.getEmail());
    }

    /*
    * 3.发送验证码
    * */
    @RequestMapping(value = "/sendsms/{mobile}")
    public Result sendSms(@PathVariable String mobile) {
        userService.sendSms(mobile);
        return new Result(0, "验证码已发送至你的手机，请注意查收");
    }

    /*
    * 4.注册
    * */
    @RequestMapping("/register/{code}")
    public Result register(@RequestBody User user , @PathVariable String code) {
        return userService.register(user,code);
    }

    /*
    *
    * model2:adminModel
    * */
    @RequestMapping("/findAllUser")
    @ResponseBody
    public Result findAllUser(int page, int limit , HttpSession session) {
        return userService.findAll(page , limit , session);
    }

    /*
    * edit user
    * */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(String id , @RequestBody User user , HttpSession session) {
        return userService.edit(id , user , session);
    }

    /*
    * change personal information
    * */
    @RequestMapping("/change")
    @ResponseBody
    public Result change(@RequestBody User user , HttpSession session) {
        return userService.change(user, session);
    }

    /*
    * promoting vip
    * */
    @RequestMapping("/promotingVip")
    @ResponseBody
    public Result promotingVip(HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        return new Result(200 , "正在跳转......" , user);
    }

    /*
     *
     * batch delete
     *  */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Result batchDelete(@RequestBody JSONArray jsonArray , HttpSession session) {
        return userService.batchDelete(jsonArray ,session);
    }

    /*
     * 微信登录中转：
     * */
    @RequestMapping("/wechatLogin")
    @ResponseBody
    public Result wechatLogin(HttpSession session) {
        return userService.wechatLogin(session);
    }

    /*
    * do vip
    * */
    @RequestMapping("/doVip")
    @ResponseBody
    public Result doVip(HttpSession session) {
        return userService.doVip(session);
    }

    /*
    * query user by condition
    * */
    @RequestMapping("/QueryUserByCondition")
    @ResponseBody
    public Result  QueryUserByCondition(Integer page, Integer limit, User user , HttpSession session) {
        return userService.QueryUserByCondition(page , limit ,user , session);
    }

    /*
    * check user IsVip
    * */
    @RequestMapping("/checkIsVip")
    @ResponseBody
    public Result checkIsVip(HttpSession session) {
        return userService.checkIsVip(session);
    }
}
